package eu.jba.denshijisho.exceptionhandler;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.Thread.UncaughtExceptionHandler;

import eu.jba.denshijisho.ErrorActivity;
import android.app.Activity;
import android.content.Intent;

public class ExceptionHandler implements UncaughtExceptionHandler{

	private final Activity context;
	
	public ExceptionHandler( Activity context ){
		this.context = context;
	}
	
	@Override
	public void uncaughtException(Thread thread, Throwable ex) {
		final StringWriter stackTrace = new StringWriter();
		ex.printStackTrace( new PrintWriter(stackTrace));
		
		final Intent i = new Intent(context, ErrorActivity.class);
		i.putExtra("error", stackTrace.toString());
		context.startActivity( i );
		android.os.Process.killProcess( android.os.Process.myPid() );
		System.exit( 10 );
	}

}
